package cn.jasonone.jfx.factory;

import cn.jasonone.jfx.config.properties.JfxProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * @author xujie
 * @version 1.0
 * @date 2021/12/22 14:07
 */
@Slf4j
@Component("resourceBundleFactory")
public class DefaultResourceBundleFactory implements ResourceBundleFactory {
    private Map<String, ResourceBundle> resourceBundleMap = new HashMap<>();
    @Resource
    private JfxProperties jfxProperties;

    @PostConstruct
    public void init() {
        Map<String, String> resourceBundleNames = jfxProperties.getResourceBundleNames();
        if(resourceBundleNames != null){
            for (Map.Entry<String, String> entry : resourceBundleNames.entrySet()) {
                ResourceBundle resourceBundle = ResourceBundle.getBundle(entry.getValue());
                log.debug("注册资源包[{}]: {}", entry.getKey(), resourceBundle);
                resourceBundleMap.put(entry.getKey(), resourceBundle);
            }
        }
    }

    @Override
    public ResourceBundle getResourceBundle(String resourceBundleName) {
        return resourceBundleMap.get(resourceBundleName);
    }
}
